package top.zackyoung.jpa.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import top.zackyoung.jpa.dao.SysRoleRepository;
import top.zackyoung.jpa.dao.SysUserRepository;
import top.zackyoung.jpa.entity.Result;
import top.zackyoung.jpa.entity.SysRole;
import top.zackyoung.jpa.entity.SysUser;
import top.zackyoung.jpa.service.UserService;

import java.util.List;
import java.util.stream.Stream;

/**
 * @author: ZackYoung
 * @time: 2020/9/18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    SysUserRepository sysUserRepository;

    @Autowired
    SysRoleRepository roleRepository;

    /**
     * 数据添加初始化
     * @return
     */
    @RequestMapping("oneKeyAdd")
    private String add(){
        try {
            userService.oneKeyAdd();
        }catch (Exception e){
            return "失败";
        }
        return "成功";
    }

    @RequestMapping("/query")
    public Result query(Result result,Integer page,Integer limit){
        Stream<SysUser> all = userService.findAll(page - 1,limit);
        int size = userService.findAll().size();
        result.setCount(size);
        result.setData(all);
        return result;
    }

    @RequestMapping("table")
    public ModelAndView table(ModelAndView mav){
        mav.setViewName("table");
        return mav;
    }
    public Result update(Result result){

        return null;
    }

    @RequestMapping("/role")
    public List<SysRole> queryRole(){
        return roleRepository.findAll();
    }
}
